package com.ztgf.shopping.service.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.shopping.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.mapper.shop.ShopContractParamRelationMapper;
import com.ztgf.shopping.models.service.shop.IShopContractParamRelationService;
import com.ztgf.shopping.models.bo.shop.ShopContractParamRelationSaveBO;
import com.ztgf.shopping.models.dto.shop.ShopContractParamRelationDTO;

/**
 * 店铺合同内容关联表控制器
 *
 * @author YUE
 * @Date 2020-07-21 11:56:39
 */
@DubboService
public class ShopContractParamRelationServiceImpl extends ServiceImpl<ShopContractParamRelationMapper, ShopContractParamRelationDTO> implements IShopContractParamRelationService{

    @Resource
    private YmlConfig ymlConfig;


    @Override
    public boolean add(ShopContractParamRelationSaveBO saveBO) {
        ShopContractParamRelationDTO dto = new ShopContractParamRelationDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    /**
     * 创建默认的店铺合同模板信息
     * @param shopId 店铺id
     */
    @Override
    public void createDefaultShopContractInfo(Long shopId){
        ShopContractParamRelationDTO shopContractParamRelationDTO = new ShopContractParamRelationDTO();
        for (int i = 0; i < 2; i++) {
            shopContractParamRelationDTO.setShopId(shopId);
            shopContractParamRelationDTO.setType(i);
            shopContractParamRelationDTO.setDeliverGoodsDate(15);
            shopContractParamRelationDTO.setGuaranteeWay("无");
            shopContractParamRelationDTO.setInformDate("2");
            shopContractParamRelationDTO.setLiquidatedDamagesRatio(0.1D);
            shopContractParamRelationDTO.setDamageRatio(3D);
            shopContractParamRelationDTO.setDeliverGoodsLiquidatedDateRatio(0.1D);
            shopContractParamRelationDTO.setDeliverGoodsPostponedLiquidatedDate(10);
            shopContractParamRelationDTO.setDeliverGoodsPostponedLiquidatedDateRatio(10D);
            shopContractParamRelationDTO.setGoodsQualityRatio(10D);
            shopContractParamRelationDTO.setIrresistibleDate(2);
            if(i == 0){//现货
                shopContractParamRelationDTO.setContractName("现货合同");
            }else{
                shopContractParamRelationDTO.setContractName("预售合同");
            }

            Wrapper wrapper = new EntityWrapper();
            wrapper.eq("shop_id", shopId);
            wrapper.eq("type",i);
            ShopContractParamRelationDTO shopContractParamRelationDTOS = this.selectOne(wrapper);
            if(null == shopContractParamRelationDTOS){
                Long id = ymlConfig.getPkId();
                shopContractParamRelationDTO.setId(id);
                insert(shopContractParamRelationDTO);
            }
        }
    }

    /**
     * 创建默认的店铺赊销合同模板信息
     * @param shopId 店铺id
     */
    @Override
    public void createDefaultShopContractInfoByCredit(Long shopId){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("shop_id", shopId);
        wrapper.eq("type",2);
        ShopContractParamRelationDTO shopContractParamRelationDTO = this.selectOne(wrapper);
        if(null == shopContractParamRelationDTO){
            shopContractParamRelationDTO = new ShopContractParamRelationDTO();
            shopContractParamRelationDTO.setShopId(shopId);
            //赊销合同类型
            shopContractParamRelationDTO.setType(2);
            shopContractParamRelationDTO.setDeliverGoodsDate(15);
            shopContractParamRelationDTO.setGuaranteeWay("无");
            shopContractParamRelationDTO.setInformDate("2");
            shopContractParamRelationDTO.setLiquidatedDamagesRatio(0.1D);
            shopContractParamRelationDTO.setDamageRatio(3D);
            shopContractParamRelationDTO.setDeliverGoodsLiquidatedDateRatio(0.1D);
            shopContractParamRelationDTO.setDeliverGoodsPostponedLiquidatedDate(10);
            shopContractParamRelationDTO.setDeliverGoodsPostponedLiquidatedDateRatio(10D);
            shopContractParamRelationDTO.setGoodsQualityRatio(10D);
            shopContractParamRelationDTO.setIrresistibleDate(2);
            shopContractParamRelationDTO.setContractName("赊销合同");
            Long id = ymlConfig.getPkId();
            shopContractParamRelationDTO.setId(id);
            insert(shopContractParamRelationDTO);
        }
    }

    /**
     * 根据店铺id和订单类型查询
     * @param shopId
     * @param type
     * @return
     */
    @Override
    public ShopContractParamRelationDTO getByShopIdAndType(Long shopId,int type){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("shop_id", shopId);
        wrapper.eq("type",type);
        ShopContractParamRelationDTO shopContractParamRelationDTO = selectOne(wrapper);
        //针对赊销合同，如果未生成则直接生成
        if(null == shopContractParamRelationDTO && type == 2){
            shopContractParamRelationDTO = new ShopContractParamRelationDTO();
            shopContractParamRelationDTO.setShopId(shopId);
            //赊销合同类型
            shopContractParamRelationDTO.setType(2);
            shopContractParamRelationDTO.setDeliverGoodsDate(15);
            shopContractParamRelationDTO.setGuaranteeWay("无");
            shopContractParamRelationDTO.setInformDate("2");
            shopContractParamRelationDTO.setLiquidatedDamagesRatio(0.1D);
            shopContractParamRelationDTO.setDamageRatio(3D);
            shopContractParamRelationDTO.setDeliverGoodsLiquidatedDateRatio(0.1D);
            shopContractParamRelationDTO.setDeliverGoodsPostponedLiquidatedDate(10);
            shopContractParamRelationDTO.setDeliverGoodsPostponedLiquidatedDateRatio(10D);
            shopContractParamRelationDTO.setGoodsQualityRatio(10D);
            shopContractParamRelationDTO.setIrresistibleDate(2);
            shopContractParamRelationDTO.setContractName("赊销合同");
            Long id = ymlConfig.getPkId();
            shopContractParamRelationDTO.setId(id);
            insert(shopContractParamRelationDTO);
        }
        return shopContractParamRelationDTO;
    }

}
